package cn.ekuma.epos.datalogic.define.dao.crm;

import cn.ekuma.data.dao.BaseDAO;
import cn.ekuma.epos.db.table.crm.I_CustomerProduct;

import com.openbravo.bean.BusinessPartner;
import com.openbravo.bean.Product;
import com.openbravo.bean.crm.CustomerProduct;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class CustomerProductDAO  extends BaseDAO<CustomerProduct>{

	public CustomerProductDAO(I_Session s) {
		super(s);
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s,
                "CUSTOMER_PRODUCT",
                new Field[]{
                     new Field(I_CustomerProduct.ID,Datas.STRING,Formats.STRING),
                     new Field(I_CustomerProduct.CUSTOMER_ID,Datas.STRING,Formats.STRING,BusinessPartner.class),
                     new Field(I_CustomerProduct.PRODUCT_ID,Datas.STRING,Formats.STRING,Product.class),
                     new Field(I_CustomerProduct.PRICE_IN,Datas.DOUBLE,Formats.DOUBLE),
                     new Field(I_CustomerProduct.TIME_IN,Datas.TIMESTAMP,Formats.TIMESTAMP),
                     new Field(I_CustomerProduct.REASON_IN,Datas.STRING,Formats.STRING),
                     new Field(I_CustomerProduct.PRICE_OUT,Datas.DOUBLE,Formats.DOUBLE),
                     new Field(I_CustomerProduct.TIME_OUT,Datas.TIMESTAMP,Formats.TIMESTAMP),
                     new Field(I_CustomerProduct.REASON_OUT,Datas.STRING,Formats.STRING),
                     new Field(I_CustomerProduct.CONTRACTPRICE,Datas.DOUBLE,Formats.DOUBLE)
                    },
                new int[]{0});
	}

	@Override
	public void writeInsertValues(DataWrite dp, CustomerProduct obj)
			throws BasicException {
		dp.setString(1,obj.getId());
		dp.setString(2,obj.getCustomerId());
		dp.setString(3,obj.getProductId());
		dp.setDouble(4,obj.getPriceIn());
		dp.setTimestamp(5,obj.getTimeIn());
		dp.setString(6,obj.getReasonIn());
		dp.setDouble(7, obj.getPriceOut());
		dp.setTimestamp(8,obj.getTimeOut());
		dp.setString(9,obj.getReasonOut());
		dp.setDouble(10,obj.getContractPrice());
	}

	@Override
	public Class getSuportClass() {
		return CustomerProduct.class;
	}

	@Override
	public CustomerProduct readValues(DataRead dr, CustomerProduct obj)
			throws BasicException {
		if(obj==null)
			obj=new CustomerProduct();
		obj.setId(dr.getString(1));
		obj.setCustomerId(dr.getString(2));
		obj.setProductId(dr.getString(3));
		obj.setPriceIn(dr.getDouble(4));
		obj.setTimeIn(dr.getTimestamp(5));
		obj.setReasonIn(dr.getString(6));
		obj.setPriceOut(dr.getDouble(7));
		obj.setTimeOut(dr.getTimestamp(8));
		obj.setReasonOut(dr.getString(9));
		obj.setContractPrice(dr.getDouble(10));
		return obj;
	}

	public void productUpdateIn(final CustomerProduct product) throws BasicException {
		int updateresult; // si ATTRIBUTESETINSTANCE_ID is null
		updateresult = new PreparedSentence(
					s,
					"UPDATE CUSTOMER_PRODUCT SET PRICE_IN =?,TIME_IN=?,REASON_IN=? WHERE CUSTOMER_ID = ? AND PRODUCT_ID = ? ",
					SerializerWriteParams.INSTANCE).exec(new DataParams() {
				@Override
				public void writeValues() throws BasicException {
					setDouble(1, product.getPriceIn());
					setTimestamp(2,product.getTimeIn());
					setString(3, product.getReasonIn());
					setString(4, product.getCustomerId());
					setString(5, product.getProductId());

				}
			});
		if (updateresult == 0) {
			insert(product);
		}
	}
	
	public void productUpdateOut(final CustomerProduct product) throws BasicException {
		int updateresult; // si ATTRIBUTESETINSTANCE_ID is null
		updateresult = new PreparedSentence(
					s,
					"UPDATE CUSTOMER_PRODUCT SET PRICE_OUT =?,TIME_OUT=?,REASON_OUT=? WHERE CUSTOMER_ID = ? AND PRODUCT_ID = ? ",
					SerializerWriteParams.INSTANCE).exec(new DataParams() {
				@Override
				public void writeValues() throws BasicException {
					setDouble(1, product.getPriceOut());
					setTimestamp(2,product.getTimeOut());
					setString(3, product.getReasonOut());
					setString(4, product.getCustomerId());
					setString(5, product.getProductId());
				}
			});
		if (updateresult == 0) {
			insert(product);
		}
	}
	
}
